/*
* 二维码扫描
* */

import 'package:barcode_scan2/model/scan_options.dart';
import 'package:barcode_scan2/platform_wrapper.dart';
import 'package:center_beijing/common/logutil.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class QrScan {
  static scan(context, {type = 1}) async {
    var options = ScanOptions(
      // set the options
      strings: {
        'cancel': '取消',
        'flash_on': '闪光灯开',
        'flash_off': '闪光灯关',
      },
    );
    var result = await BarcodeScanner.scan(options: options);
    print(result.rawContent); // The barcode content
    Uri u = Uri.parse(result.rawContent.replaceAll('#/', ''));
    var passResult;
    if(result.rawContent==''){
      return passResult;
    }
    if(result.rawContent.contains('rehaview/share')){
      return result.rawContent;
    }
    if(result.rawContent.contains('employeeId')){
      return result.rawContent;
    }
    if (u.queryParameters['source'] == 'rhea') {
      if(type==1||type==3){
        if (u.queryParameters['type'] == '1') {
          if (u.queryParameters['projectId'] ==
              GlobalConfig.selectFactory.value) {
            passResult = {
              'type': 1,
              'code': u.queryParameters['id'],
              'projectId': u.queryParameters['projectId']
            };
          } else {
            showDialog(
                barrierDismissible: false,
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text('注意'),
                    content: Text('构件不在当前工厂下！'),
                    actions: <Widget>[
                      new Container(
                        child: FlatButton(
                          child: new Text(
                            "确定",
                            style: TextStyle(color: Colors.red),
                          ),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                      ),
                    ],
                  );
                });
          }
        } else if (u.queryParameters['type'] == '2') {
          passResult = {'type': 3, 'code': u.queryParameters['code']};
        }
      }else{
        showDialog(
            barrierDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('注意'),
                content: Text('二维码格式不正确！'),
                actions: <Widget>[
                  new Container(
                    child: FlatButton(
                      child: new Text(
                        "确定",
                        style: TextStyle(color: Colors.red),
                      ),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                  ),
                ],
              );
            });
      }
    } else if (u.queryParameters['qrCodeType'] != null) {
      if(type==3||type==2){
        if (u.queryParameters['qrCodeType'] == '3') {
          if (u.queryParameters['projectSectionId'] ==
              GlobalConfig.selectFactory.tag) {
            passResult = {'type': 2, 'code': u.queryParameters['id']};
          } else {
            showDialog(
                barrierDismissible: false,
                context: context,
                builder: (BuildContext context) {
                  return AlertDialog(
                    title: Text('注意'),
                    content: Text('二维码格式不正确！'),
                    actions: <Widget>[
                      new Container(
                        child: FlatButton(
                          child: new Text(
                            "确定",
                            style: TextStyle(color: Colors.red),
                          ),
                          onPressed: () {
                            Navigator.pop(context);
                          },
                        ),
                      ),
                    ],
                  );
                });
          }
        } else {
          showDialog(
              barrierDismissible: false,
              context: context,
              builder: (BuildContext context) {
                return AlertDialog(
                  title: Text('注意'),
                  content: Text('请扫描设备二维码！'),
                  actions: <Widget>[
                    new Container(
                      child: FlatButton(
                        child: new Text(
                          "确定",
                          style: TextStyle(color: Colors.red),
                        ),
                        onPressed: () {
                          Navigator.pop(context);
                        },
                      ),
                    ),
                  ],
                );
              });
        }
      }else{
        showDialog(
            barrierDismissible: false,
            context: context,
            builder: (BuildContext context) {
              return AlertDialog(
                title: Text('注意'),
                content: Text('二维码格式不正确！'),
                actions: <Widget>[
                  new Container(
                    child: FlatButton(
                      child: new Text(
                        "确定",
                        style: TextStyle(color: Colors.red),
                      ),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    ),
                  ),
                ],
              );
            });
      }

    } else {
      showDialog(
          barrierDismissible: false,
          context: context,
          builder: (BuildContext context) {
            return AlertDialog(
              title: Text('注意'),
              content: Text('二维码格式不正确！'),
              actions: <Widget>[
                new Container(
                  child: FlatButton(
                    child: new Text(
                      "确定",
                      style: TextStyle(color: Colors.red),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
              ],
            );
          });
    }
    return passResult;
  }
}
